package queue;

import java.util.LinkedList;
import java.util.Queue;
//队列实现栈
class MyStack {
    public Queue<Integer> q1;
    public Queue<Integer> q2;

    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }


    public void push(int x) {
        if(!q1.isEmpty()){
            q1.offer(x);
        } else if (!q2.isEmpty()) {
            q2.offer(x);
        }else{
            //第一次offer时两个栈都为空，此时默认放入第一个栈
            q1.offer(x);
        }

    }

    public int pop() {
        if (empty()){
            return -1;
        }
        if (!q1.isEmpty()){
            int size = q1.size();//存一下
            for (int i = 0; i < size-1; i++) {
                int x = q1.poll();
                q2.offer(x);
                
            }
            return q1.poll();
        }else{
            int size = q2.size();
            for (int i = 0; i < size-1; i++) {
                int x = q2.poll();
                q1.offer(x);

            }
            return q2.poll();

        }

    }

    public int top() {
        if (empty()){
            return -1;
        }
        if (!q1.isEmpty()){
            int size = q1.size();//存一下
            int x = -1;
            for (int i = 0; i < size; i++) {
                x = q1.poll();
                q2.offer(x);

            }
            return x;
        }else{
            int size = q2.size();
            int x = -1;
            for (int i = 0; i < size; i++) {
                x = q2.poll();
                q1.offer(x);

            }
            return x;

        }


    }

    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();

    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */